import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024 —03 —28
 * Time: 20:42
 */
public class MySingleList  implements IList{

    static class ListNode{
        //静态成员变量可以直接调用
        public int value;//数据域中的数据
        public ListNode next;//next是一个引用存的是地址，类型是ListNode类型
        /*
        * 在实例化一个ListNode类的时候next的值默认为null
        * */

        public ListNode(int value){
            this.value = value;//这里没有对ListNode进行构造是因为实例化一个对象后，节点还没有插入到具体的位置，不知道next存的是谁的地址
        }
    }

    public ListNode head;//本身是引用，存的是地址

    /*
    * 创建一个链表
    * */
    public void createList(){
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(56);
        ListNode node5 = new ListNode(67);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;
    }







    @Override
    public void addfirst(int data) {
        ListNode node = new ListNode(data);//先把这个插入头部的节点创建出来
        ListNode current = this.head;
        node.next = current;
        this.head = node;
    }

    @Override
    public void addlast(int data) {
        ListNode node = new ListNode(data);
        ListNode current = this.head;
        if(this.head == null){
            this.head = node;
        }else{
            while (current.next !=null){
                current = current.next;
            }
            current.next = node;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        if(index < 0 || index >size() ){
            throw new IndexOutOfBoundsException("传送位置超出范围");
            //抛出异常 就这么写  throw + new + 方法名字
        }
        if (index == 0){
            addfirst(data);
            return;
        }
        if (index == size()){
            addlast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode current = this.head;
        for (int i = 0; i < index - 1; i++) {    //index是在哪个下标插入元素，比如第二个位置插入就让current走一步
            current = current.next;
        }
        node.next = current.next;
        current.next = node;

    }

    @Override
    public void contains(int key) {

    }

    @Override
    public void remove(int key) {

        if (this.head == null){
            return;
        }
        if (this.head != null){//找的key在第一个节点处
            while (this.head.value == key){
                //current = null;不能这么写运行时会报空指针异常
                this.head = this.head.next;//也不能用curren来删除它只是一个中间变量
                return;
            }
        }
        ListNode current = findPrev(key);
        if (current == null){
            System.out.println("没有找到你输入的数据"+ " : " +key);
            return;
        }
        ListNode del = current.next;
        current.next = del.next;
    }

    public ListNode findPrev(int key){//找value为key的上一个节点
        ListNode current = this.head;
        while (current.next != null){//遍历链表从第一个开始判断
            if(current.next.value == key){//current.value == key这么写最后一个数据value拿不到了
                return current;
            }
            current = current.next;
        }
           return null;
    }
    @Override
    public void removeaAllKey(int key) {

    }

    @Override
    public int size() {
        ListNode current = this.head;
        int count = 0;
        while (current != null){
            current = current.next;
            count++;
        }
        return count;
    }

    @Override
    public void clear() {

    }

    @Override
    public void display() {
        /*
        * 得找一个中间变量来替代head
        * 否则打印完成后再去找第一个节点的位置就找不到了
        * 此时head的位置并不在第一个节点*/
        ListNode current = head;
        while(current != null){
            System.out.print(current.value+" ");
            current = current.next;
        }
        System.out.println();//换行
        return;
    }
}
